import scrapy
import json
from ..items import KfcItem


class KfcSpider(scrapy.Spider):
    name = "kfc"
    allowed_domains = ["www.kfc.com.cm"]
    post_urls = "http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword"
    city_name = '广东'  # input('请输入城市名称：')

    def start_requests(self):
        formdata = {
            'cname': '',
            'pid': '',
            'keyword': self.city_name,
            'pageIndex': '1',
            'pageSize': '10',
        }
        yield scrapy.FormRequest(url=self.post_urls, formdata=formdata, callback=self.get_total, dont_filter=True)

    def get_total(self, response):
        html = json.loads(response.text)
        count = html['Table'][0]['rowcount']
        total_page = count // 10 if count % 10 == 0 else count // 10 + 1
        for page in range(1, total_page + 1):
            formdata = {
                'cname': '',
                'pid': '',
                'keyword': self.city_name,
                'pageIndex': str(page),
                'pageSize': '10',
            }
            yield scrapy.FormRequest(url=self.post_urls, formdata=formdata, callback=self.parse, dont_filter=True)

    def parse(self, response):
        html = json.loads(response.text)
        for one_shop_dict in html['Table1']:
            item = KfcItem()
            item['rownum'] = one_shop_dict['rownum']
            item['storeName'] = one_shop_dict['storeName']
            item['addressDetail'] = one_shop_dict['addressDetail']
            item['cityName'] = one_shop_dict['cityName']
            item['provinceName'] = one_shop_dict['provinceName']

            yield item
